package com.bryantkobe.myspringcode.exception;

import java.io.Serial;

/**
 * @description: 参数校验异常
 * @author: alienwarezzl
 * @date: 2025/9/22
 */
public final class ValidationException extends BusinessException implements BusinessExceptionType{
    @Serial
    private static final long serialVersionUID = 1L;

    // 校验失败的字段信息
    private final String fieldName;
    private final Object rejectedValue;

    public ValidationException(String fieldName, Object rejectedValue, String message) {
        super("VALIDATION_ERROR",
                String.format("字段'%s'校验失败: %s", fieldName, message));
        this.fieldName = fieldName;
        this.rejectedValue = rejectedValue;

        // 自动添加参数上下文
        withParam("field", fieldName)
                .withParam("rejectedValue", rejectedValue)
                .withParam("validationRule", message);
    }

    @Override
    public String getCategory() {
        return "VALIDATION";
    }

    public String getFieldName() {
        return fieldName;
    }

    public Object getRejectedValue() {
        return rejectedValue;
    }
}
